//
//  upload.swift
//  股票框架
//
//  Created by 李小骥 on 2017/10/23.
//  Copyright © 2017年 李小骥. All rights reserved.
//

import Alamofire
import Foundation
import UIKit
import SwiftyJSON
import Photos
private let NetworkRequestShareInstance = NetworkRequest()

class NetworkRequest {
    class var sharedInstance : NetworkRequest {
        return NetworkRequestShareInstance
    }
}
extension NetworkRequest {
    //MARK: - 照片上传的封装
    ///
    /// - Parameters:
    ///   - urlString: 服务器地址
    ///   - params: ["flag":"","userId":""] - flag,userId       为上传的参数
    ///        flag - 666 信息上传多张   －000 头像上传
    ///   - data: image转换成Data
    ///   - name: fileName
    ///   - success:
    ///   - failture:、
    
    func upLoadImageRequest(urlString : String, params:[String:String], data: [Data], name: [String],success : @escaping (_ response : [String : AnyObject])->(), failture : @escaping (_ error : Error)->()){
        //header的上传
        let headers = ["content-type":"multipart/form-data"]
        Alamofire.upload(multipartFormData: { multipartFormData in
            //666多张图片上传
            let flag = params["flag"]
            let userId = params["userId"]
            
            //参数的上传
            multipartFormData.append((flag?.data(using: String.Encoding.utf8)!)!, withName: "flag")
            multipartFormData.append( (userId?.data(using: String.Encoding.utf8)!)!, withName: "userId")
            
            //multipartFormData.append(<#T##data: Data##Data#>, withName: <#T##String#>, fileName: <#T##String#>, mimeType: <#T##String#>)  ---使用的方法
            //遍历图片，挨个添加，withName-name和后台确定是什么
            for i in 0..<data.count {
                multipartFormData.append(data[i], withName: "appPhoto", fileName: name[i], mimeType: "image/png")
            }
        },
                         to: urlString,
                         headers: headers,
                         encodingCompletion: { encodingResult in
                            switch encodingResult {
                            case .success(let upload, _, _):
                                upload.responseJSON { response in
                                    if let value = response.result.value as? [String: AnyObject]{
                                        success(value)
                                    }
                                }
                            case .failure(let encodingError):
                                failture(encodingError)
                            }
        }
        )
    }
}
func bm_upload_photo_info(params:[String: String], data: [Data], name: [String],
                                 success:@escaping (_ response:[String:AnyObject]) -> (), failure:@escaping(_ error: Error) -> ()){
    NetworkRequest.sharedInstance.upLoadImageRequest(urlString: "服务器地址",params:params, data: data, name: name, success: { (response) in
        success(response)
    }, failture: { (error) in
        failure(error)
    })
}
//照片上传
//MARK: - 照片上传
///
/// - Parameters:
///   - urlString: 服务器地址
///   - params: ["flag":"","userId":""] - flag,userId 为必传参数
///        flag - 666 信息上传多张  －999 服务单上传  －000 头像上传
///   - data: image转换成Data
///   - name: fileName
///   - success:
///   - failture:
func upLoadImageRequest(urlString : String, params:[String:String], imageData: Data, imageKey:String, imageFileName: String,success : @escaping (_ response : [String : AnyObject])->(), failture : @escaping (_ error : Error)->()){
    
    let headers = ["content-type":"multipart/form-data"]
    
    Alamofire.upload(
        multipartFormData: { multipartFormData in
            //666多张图片上传
//            for (key , value) in params{
//
//                multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
//            }
//            let uuid = params["uuid"]!
//            let sign = params["sign"]!
//            multipartFormData.append((uuid.data(using: String.Encoding.utf8)!), withName: "uuid")
//            multipartFormData.append((sign.data(using: String.Encoding.utf8)!), withName: "sign")
            for (key,value) in params{
                multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
            }
//            multipartFormData.append(data, withName: "header_img_url", fileName: name, mimeType: "image/png")
            multipartFormData.append(imageData, withName: imageKey)

    },
        to: urlString,
        headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    if let value = response.result.value as? [String: AnyObject]{
                        success(value)
                        let json = JSON(value)
                        print(json)
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
                failture(encodingError)
            }
    }
    )
}

